﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Permissions;
using System.Collections;
using System.Web.UI.HtmlControls;
using System.Collections.ObjectModel;

namespace TabStrip {
    [ToolboxData("<{0}:TabStrip runat=server></{0}:TabStrip>")]
    [ParseChildren(ChildrenAsProperties=true, DefaultProperty="Items")]
    [AspNetHostingPermission(SecurityAction.Demand,
          Level = AspNetHostingPermissionLevel.Minimal)]
    public class TabControl : Panel, INamingContainer {
        private ArrayList _items = new ArrayList();

        public ArrayList Items {
            get {
                return _items;
            }
        }

        public TabControl(): base() {
            
        }

        private const string SELECTED_INDEX_PROP = "TabControl_SelectedIndex";
        private string getIndexPropFieldName() {
            return ClientID + "_" + SELECTED_INDEX_PROP;
        }
        private string getPropFieldValue() {
            return this.Page.Request.Form[getIndexPropFieldName()];
        }

        private int? _selectedIndex = null;
        public int SelectedIndex {
            get {
                if (getPropFieldValue() == null) {
                    return 0;
                } else {
                    _selectedIndex = int.Parse(getPropFieldValue());
                }
                return _selectedIndex.Value;
            }
            set {
                PreRender -= Selected_Registration;
                PreRender += Selected_Registration;
                _selectedIndex = value;
            }
        }

        protected void Selected_Registration(object sender, EventArgs e) {
            if (_selectedIndex.HasValue) {
                ScriptManager.RegisterHiddenField(this, getIndexPropFieldName(), _selectedIndex.ToString());
                PreRender -= Selected_Registration;
            }
        }

        protected override void CreateChildControls() {
            Panel tabPanel = new Panel();
            Panel tabItemPanel = new Panel();
            for (int i = 0; i < Items.Count; i++) {
                TabItem item = (TabItem) Items[i];
                item.Style[HtmlTextWriterStyle.Display] = (SelectedIndex == i ? "block" : "none");
                item.CssClass = "tabpanel" + (SelectedIndex == i ? " selected" : "");
                item.ID = "T" + i.ToString();

                tabItemPanel.CssClass = "tab_item_panel";
                tabItemPanel.Controls.Add(item);

                string itemId = item.ClientID;
                HtmlAnchor tabLabel = new HtmlAnchor();
                tabLabel.Attributes["class"] = "tablabel" + (SelectedIndex == i ? " selected" : "");

                tabLabel.ID = "L" + i.ToString();
                tabPanel.CssClass = "tab_label_panel";
                tabPanel.Controls.Add(tabLabel);
                string labelId = tabLabel.ClientID;
                tabLabel.InnerText = item.Caption;
                string thisCtlId = ClientID;
                tabLabel.HRef = "javascript:changeTabPage('" + thisCtlId + "','" + itemId + "','" + labelId + "');";
                tabLabel.Attributes.Add("onclick", "return true;");
            }
            Controls.Add(tabPanel);
            Controls.Add(tabItemPanel);
        }
    }

    [ToolboxData("<{0}:TabItem runat=server></{0}:TabItem>")]
    [ParseChildren(ChildrenAsProperties = false)]
    [AspNetHostingPermission(SecurityAction.Demand,
          Level = AspNetHostingPermissionLevel.Minimal)]
    public sealed class TabItem: Panel {
        private const string CAPTION_PROP = "TabItem_Caption";
        public string Caption {
            get {
                Object cv = ViewState[CAPTION_PROP];
                return (cv == null ? "TabItem" : (string) ViewState[CAPTION_PROP]);
            }
            set {
                ViewState[CAPTION_PROP] = value;
            }
        }
    }
}